package au.com.carboncycle.core;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import com.google.common.collect.ForwardingCollection;

/**
 * TODO: this is actually a queue - should definitely fix this
 * @author Brendan
 *
 * @param <E>
 * @deprecated Probably no need for this anymore
 */
public class RingBuffer<E> extends ForwardingCollection<E> {

	private LinkedList<E> buffer;
	private final int maxSize;
	
	
	public RingBuffer (int maxSize) {
		buffer = new LinkedList<E>();
		this.maxSize = maxSize;
	}
	
	public int getMaxSize() {
		return maxSize;
	}
	
	@Override
	protected List<E> delegate() {
		return buffer;
	}

	@Override
	public boolean add(E element) {
		synchronized (delegate()) {
			super.add(element);
			ensureBufferSize();
		}
		return true;
	}

	private void ensureBufferSize() {
		while (size() > maxSize) {
			buffer.remove(0);
		}
	}

	@Override
	public boolean addAll(Collection<? extends E> collection) {
		synchronized (delegate()) {
			super.addAll(collection);
			ensureBufferSize();
		}
		return true;
	} 
	
	
	
	
	
}
